class Request extends egret.DisplayObjectContainer {
    public statusGetLabel = new egret.TextField();
    public statusPostLabel:any;
    public callback = new Function();

    public constructor(a:Function) {
        super();
        this.callback = a;
    }

    // get请求
    public sendGetRequest():void {
        // var params = "login";
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open("http://localhost:3007/",egret.HttpMethod.GET);
        request.send();
        request.addEventListener(egret.Event.COMPLETE,this.onGetComplete,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onGetIOError,this);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onGetProgress,this);
    }

    // get请求完成
    public onGetComplete(event:egret.Event):void {
        var request = <egret.HttpRequest>event.currentTarget;
        this.callback(request);
    }


    // get请求错误
    public onGetIOError(event:egret.IOErrorEvent):void {
        console.log("get error : " + event);
    }

    // get请求过程
    public onGetProgress(event:egret.ProgressEvent):void {
        console.log("get progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
    }

    // post请求
    public sendPostRequest(params) {
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open("http://localhost:3007",egret.HttpMethod.POST);
        // request.setRequestHeader("Content-Type", "text/html");
        request.send(params);
        request.addEventListener(egret.Event.COMPLETE,this.onPostComplete,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onPostProgress,this);
    }

    // post请求完成
    public onPostComplete(event:egret.Event){
        var request = <egret.HttpRequest>event.currentTarget;
        this.callback(request);
    }

    private onPostIOError(event:egret.IOErrorEvent):void {
        console.log("post error : " + event);      
    }


    // post请求过程
    public onPostProgress(event:egret.ProgressEvent):void {
        // console.log("post progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
    }
}